<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter phplist Library
*
* CodeIgniter phplist bridge allowing adding users to lists and blacklisting them
*
* @author Georgi Budinov
* @version	1.0
* @link http://georgi.budinov.com
*/
class Phplist_library
{
    public $CI;

    /**
     * Constructor.
     */
    public function __construct()
    {
    	global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
        if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}

		$rootPath = $this->CI->config->item('root_path');

		define('BULLETIN_ROOT_PATH', $rootPath.'bulletin/');

		$phplist_root_path = (defined('PHPLIST_ROOT_PATH')) ? PHPLIST_ROOT_PATH : BULLETIN_ROOT_PATH;

		// Include needed files
		include($phplist_root_path . 'admin/defaultconfig.inc');
		include($phplist_root_path . 'config/config.php');
		include($phplist_root_path . 'admin/mysql.inc');
		include($phplist_root_path . 'admin/connect.php');
    }

    /**
     *
     * @param $email
     * @param $lists
     * @return mixed
     */
    public function user_add($email, $lists = array())
    {
    	if(!$email) return false;

    	if(is_array($lists) && count($lists))
    	{
    		$userID = addNewUser($email);

    		if($userID > 0)
    		{
    			foreach($lists as $list)
    			{
    				$this->userAddToList($userID, $list);
    			}
    		}
    	}
    	else
    	{
        	return addNewUser($email);
    	}
    }

	public function get_lists()
    {
    	$results = $this->getLists();

    	return $results;
    }

	public function user_add_list($email, $list)
    {
    	if(!$email || !$list) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		$this->userAddToList($userid, $list);
    	}
    	else
    	{
    		return;
    	}
    }

	public function user_remove_list($email, $list)
    {
    	if(!$email || !$list) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		$this->userRemoveToList($userid, $list);
    	}
    	else
    	{
    		return;
    	}
    }

    /**
     *
     * @param $email
     * @return mixed
     */
    public function user_blacklist($email)
    {
    	if(!$email) return;

    	return addUserToBlackList($email);
    }

	/**
     *
     * @param $email
     * @return mixed
     */
    public function user_unblacklist($email)
    {
    	if(!$email) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		return unBlackList($userid);
    	}
    	else
    	{
    		return false;
    	}
    }

	private function getUserIDByEmail($email = '') {
	  global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
	  if (!$email) return;

	  # workaround for integration webbler/phplist
	  if (!isset($table_prefix))
	    $table_prefix = "phplist_";

	  if (isset($tables["attribute"])) {
	    $att_table = $tables["attribute"];
	    $user_att_table = $tables["user_attribute"];
	    $usertable = $tables["user"];
	  } else {
	    $att_table = "attribute";
	    $user_att_table = "user_attribute";
	    $usertable = "user";
	  }
	  $email = mysql_real_escape_string($email, $database_connection);
	  $userid = Sql_Fetch_Row_Query("select id from {$usertable} where email = \"$email\"");
	  $id = $userid[0];

	  return $id;
	}

	private function userAddToList($id = 0, $listID = 0)
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$id = intval($id);
		$listID = intval($listID);
		if(!$id || !$listID) return false;

		$query = "replace into ".$tables["listuser"]." (userid,listid,entered) values($id,$listID,now())";
	    $result = Sql_query($query);
	}

	private function userRemoveToList($id = 0, $listID = 0)
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$id = intval($id);
		$listID = intval($listID);
		if(!$id || !$listID) return false;

		$query = "delete from ".$tables["listuser"]." where userid = $id and listid = $listID";
	    $result = Sql_query($query);
	}

	private function getLists()
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$results = array();
		$result = Sql_query("SELECT * FROM {$tables['list']} order by listorder");
		while ($row = Sql_fetch_array($result))
		{
			$results[$row['id']] = $row;
		}

		return $results;
	}
}

/* End of file phplist_library.php */
/* Location: ./application/libraries/phplist_library.php */
